package edu.uta.cfl.GUI;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.regex.Pattern;

public class TrialFile {
	static String[] rank = new String[300];
	static ArrayList<String> rank1 = new ArrayList<String>();
	static ArrayList<String> p1Name = new ArrayList<String>();
	static ArrayList<String> p1Value = new ArrayList<String>();
	static ArrayList<String> p2Name = new ArrayList<String>();
	static ArrayList<String> p2Value = new ArrayList<String>();
	static ArrayList<String> soc1 = new ArrayList<String>();
	static ArrayList<String> soe1 = new ArrayList<String>();
	static ArrayList<String> finalOutput = new ArrayList<String>();
	static String[] parameter1 = new String[300];
	static String[] parameter2 = new String[300];
	String[] soc = new String[300];
	String[] soe = new String[300];

	public static void main(String[] args) {
		String line = "";
		// Read the file
		try {
			BufferedReader br = new BufferedReader(new FileReader(
					"/Users/jagan/Desktop/sc.txt"));
			try {
				while ((line = br.readLine()) != null) {
					if (line.startsWith("2-way")) {
						// String[] rank = new String[300];
						// ArrayList <String> rank1 = new ArrayList<String>();
						// ArrayList <String> p1Name = new ArrayList<String>();
						// ArrayList<String> p1Value = new ArrayList<String>();
						// ArrayList<String> p2Name = new ArrayList<String>();
						// ArrayList<String> p2Value = new ArrayList<String>();
						// ArrayList<String> soc1 = new ArrayList<String>();
						// ArrayList<String> soe1 = new ArrayList<String>();
						// String[] parameter1 = new String[300];
						// String[] parameter2 = new String[300];
						// String[] soc = new String[300];
						// String[] soe = new String[300];
						int i = 0;
						while ((line = br.readLine()) != null) {
							// Here, we remove the ( & ) from the output file
							line = line.replace("(", "");
							line = line.replace(")", "");
							// now we have to split results based on the comma's
							// and add the
							// store them in the individual array list
							Pattern paramPattern = Pattern.compile(",");
							String[] tokens = paramPattern.split(line);
							rank1.add(tokens[0].toString()); // storing the rank
							parameter1[i] = tokens[1]; // we will need to split
														// the parameter further
														// as parameter name and
														// parameter value
							parameter2[i] = tokens[2];
							soc1.add(tokens[3].toString()); // storing the SoC
															// value
							soe1.add(tokens[4].toString()); // storing the SoE
															// value
							// We will have to sepearte the parameter name and
							// parameter value from (p1=1) as "p1", "1"
							Pattern valuePattern = Pattern.compile("=");
							String[] parameter1 = valuePattern.split(tokens[1]);
							String[] parameter2 = valuePattern.split(tokens[2]);
							p1Name.add(parameter1[0]); // storing the parameter
														// 1 name
							p1Value.add(parameter1[1]); // storing the parameter
														// 1 value
							p2Name.add(parameter2[0]); // storing the parameter
														// 2 name
							p2Value.add(parameter2[1]); // storing the parameter
														// 2 value
						}// end of inner most while loop
					} // end of if loop for "2-way"
				} // end of inner while
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} // catch for inner try block ends
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // catch for outer TRY block ends
			// We get the size of rank. This gives an idea how much records are
			// there in the output file.
		// in future, we will need to introduce validation to ensure size of all
		// the elements are equal.
		int sizeOfList = rank1.size();

		for (int a = 0; a < sizeOfList; a++) {
			// Now, to store all the results in a single ArrayList<String>
			finalOutput.add((rank1.get(a) + "," + p1Name.get(a) + ","
					+ p1Value.get(a) + "," + p2Name.get(a) + ","
					+ p2Value.get(a) + "," + soc1.get(a) + "," + soe1.get(a)));
		}

		Vector<Object> row1 = new Vector<Object>();
		for (int i = 0; i < finalOutput.size(); i++) {
			row1.add(finalOutput.get(i));
		}
	}
}
